package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.example.dao.category.CategoryMapper;
import org.example.entity.Category;
import org.example.entity.PageBean;
import org.example.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;
    /**
     * 删除种类
     * */
    @Override
    public boolean delete(Integer categoryId) {
        return categoryMapper.delete(categoryId);
    }
    /**
     * 分页按条件获取种类
     * */
    @Override
    public PageBean<Category> categoryByPage(Integer pageSize, Integer page, Integer categoryId, String categoryName, Integer category) {
        PageHelper.startPage(page,pageSize);
        List<Category> list = categoryMapper.listByReason(categoryId,categoryName,category);

        Page<Category> p = (Page<Category>) list;

        long total = p.getTotal();
        list = p.getResult();
        return new PageBean<Category>(total,list);
    }


    /**
     * 新增种类
     * */
    @Override
    public boolean add(Category category) {
        try {
            return categoryMapper.add(category);
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 修改种类信息
     * */
    @Override
    public void update(Category category, Integer categoryId) {
        category.setCategoryId(categoryId);
        categoryMapper.update(category);
    }

    /**
     * 获取所有菜品类型名
     * */
    @Override
    public List<String> categoryList() {
        return categoryMapper.list();
    }

    /**
     * 获取类别全部信息*/
    @Override
    public List<Category> allCategoryList() {
        return categoryMapper.allList();
    }
}
